package com.ruoyi.examation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.examation.domain.ExamRegistration;
import com.ruoyi.examation.domain.ExamRegistrationUser;
import com.ruoyi.examation.domain.ExamSchedule;
import com.ruoyi.examation.vo.ExamRegistrationUserVo;
import com.ruoyi.examation.vo.ExamScheduleVo;
import com.ruoyi.examation.mapper.ExamScheduleMapper;
import com.ruoyi.examation.service.IExamScheduleService;
import com.ruoyi.examation.service.IExamRegistrationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 考试时间安排Service业务层处理
 *
 * @author Tellsea
 * @date 2025-10-10
 */
@Service
public class ExamScheduleServiceImpl extends ServiceImpl<ExamScheduleMapper, ExamSchedule> implements IExamScheduleService {

    @Autowired
    private IExamRegistrationService examRegistrationService;

    @Override
    public TableDataInfo<ExamScheduleVo> queryList(ExamScheduleVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<ExamScheduleVo> queryAll(ExamScheduleVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public ExamScheduleVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public List<ExamScheduleVo> queryEnabledSchedulesWithRegistrationCount(Long examId) {
        // 查询指定考试的所有启用时段
        List<ExamSchedule> schedules = this.lambdaQuery()
                .eq(ExamSchedule::getExamId, examId)
                .eq(ExamSchedule::getIsEnabled, "1")
                .list();

        // 转换为VO并统计报名人数
        List<ExamScheduleVo> scheduleVos = new ArrayList<>();
        for (ExamSchedule schedule : schedules) {
            ExamScheduleVo vo = new ExamScheduleVo();
            // 复制基本属性
            BeanUtils.copyProperties(schedule, vo);
            
            // 统计该时段的报名人数
            long registrationCount = examRegistrationService.lambdaQuery()
                    .eq(ExamRegistration::getExamId, examId)
                    .eq(ExamRegistration::getScheduleId, schedule.getId())
                    .count();
            
            vo.setRegistrationCount((int) registrationCount);
            scheduleVos.add(vo);
        }
        
        return scheduleVos;
    }

    @Override
    public List<ExamRegistrationUser> getExamRegistrationList(Long examId) {
        //首先获取这门考试开放的考试时段id列表
        // 查询指定考试的所有启用时段
        List<ExamSchedule> schedules = this.lambdaQuery()
                .eq(ExamSchedule::getExamId, examId)
                .eq(ExamSchedule::getIsEnabled, "1")
                .list();
        List<ExamRegistrationUser> result = new ArrayList<>();
        //然后根据这些时段id列表，查询每个时段的报名人员列表
        for (ExamSchedule schedule : schedules) {
            //首先构建每一个考试时段的
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String examScheduleName = sdf.format(schedule.getStartTime()) + " - " + sdf.format(schedule.getEndTime());
            List<ExamRegistrationUserVo> registrationUsers = examRegistrationService.queryRegistrationUsersBySchedule(examId, schedule.getId());
            for(int i = 0;i<registrationUsers.size();i++){
                ExamRegistrationUser user = new ExamRegistrationUser();
                user.setIndex(String.valueOf(i + 1));
                user.setUserName(registrationUsers.get(i).getUserName());
                user.setNickName(registrationUsers.get(i).getNickName());
                user.setPhonenumber(registrationUsers.get(i).getPhonenumber());
                user.setRegistrationTime(registrationUsers.get(i).getRegistrationTime());
                user.setExamScheduleName(examScheduleName);
                result.add(user);
                //如果是最后一个元素
                if(i == registrationUsers.size() - 1){
                    ExamRegistrationUser emptyUser = new ExamRegistrationUser();
                    result.add(emptyUser);
                }
            }
        }
        return result;
    }
}
